<template>
  <!-- <div class="container" style="width: 100%; height: 100%;"> -->
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span class="active">会员管理</span>
    </div>

    <el-row>
      <el-col :span="4">
        <span>关键字：</span>
        <el-input class="searchInp" placeholder="关键字" v-model="query.key"></el-input>
      </el-col>
      <el-col :span="8">
        <el-button type="primary" class="searchbtn" size="small" @click="search">查询</el-button>
      </el-col>
    </el-row>

    <el-row :gutter="10" class="btns">
      <el-button type="primary" class="searchbtn ico" size="small" @click="handleBatchDelete">批量删除</el-button>
    </el-row>

    <!--  表格 -->

    <BaseTable
      :is-show-selection="true"
      :tableColumn="tableColumn"
      :tableData="tableData"
      :loading="loading"
      @edit="handleClick"
      @delete="handleDelete"
      @selection-change="handleSelectionChange"
    >
      <template #options="{ row }">
        <el-button size="small" @click="handleDelete(row)" type="danger">删除</el-button>
      </template>
    </BaseTable>

    <!-- 分页 -->
    <el-pagination
      class="pagination"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="query.page"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="query.psize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="counts"
    >
    </el-pagination>

    <!-- 添加/编辑弹窗 -->
  </el-card>
  <!-- </div> -->
</template>

<script>
import { list, del, batchDelete } from '../api/member'
import BaseTable from '@/components/BaseTable.vue'
export default {
  name: 'MemberView',
  components: {
    BaseTable,
  },
  data() {
    return {
      loading: true,
      query: {
        page: 1,
        psize: 5,
        key: '',
        services: '',
      },
      counts: 0,
      tableColumn: [
        {
          prop: 'id',
          label: 'id',
          width: 180,
        },
        {
          prop: 'name',
          label: '姓名',
          width: 180,
        },
        {
          prop: 'mobile',
          label: '电话',
        },
        {
          prop: 'sex',
          label: '性别',
          slotname: 'sex'
        },
        {
          prop: 'photo',
          label: '图片',
          slotname: 'photo'
        },
        {
          prop: 'addtime',
          label: '发布时间',
        },
        {
          prop: 'addip',
          label: 'IP地址',
        },
        {
          prop: 'options',
          label: '操作',
          slotname: 'options',
        },
      ],
      gridData: [],
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        name: '',
        region: '', 
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: '',
      },
      serviceList: [], // 服务范围列表
      formLabelWidth: '120px',

      currentPage1: 5,
      currentPage2: 5,
      currentPage3: 5,
      currentPage4: 4,
      formInline: {
        user: '',
        region: '',
      },
      tableData: [],
      multipleSelection: [],
    }
  },

  methods: {
    handleClick(row) {
      console.log('编辑操作:', row)
      // 这里可以添加编辑逻辑
    },
    async handleDelete(row) {
      try {
        await this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
        await del({ id: row.id })
        await this.getList()
        this.$message({
          type: 'success',
          message: '删除成功!',
        })
      } catch (error) {
        if (error === 'cancel') {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        } else {
          console.error('删除失败:', error)
        }
      }
    },
    async handleBatchDelete() {
      if (!this.multipleSelection || this.multipleSelection.length === 0) {
        this.$message({
          type: 'warning',
          message: '请至少选择一条记录!',
        })
        return
      }

      try {
        await this.$confirm(`此操作将永久删除选中的 ${this.multipleSelection.length} 条记录, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })

        // 提取选中项的ID数组
        const ids = this.multipleSelection.map((item) => item.id)

        // 调用批量删除接口
        await batchDelete({ ids })

        // 刷新列表
        await this.getList()

        this.$message({
          type: 'success',
          message: '批量删除成功!',
        })
      } catch (error) {
        if (error === 'cancel') {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        } else {
          console.error('批量删除失败:', error)
          this.$message({
            type: 'error',
            message: '批量删除失败!',
          })
        }
      }
    },
    async getList() {
      this.loading = true
      try {
        let {
          data: { counts, list: listData },
        } = await list(this.query)
        this.counts = counts
        this.tableData = listData
        this.loading = false
        console.log('获取到的数据:', listData)
      } catch (error) {
        console.error('获取数据失败:', error)
      }
    },


    onSubmit() {
      console.log('查询表单提交:', this.formInline)
      // 这里可以添加实际的查询逻辑
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      console.log('选中项发生变化:', val)
    },
    handleEdit(row) {
      // 跳转到编辑页面，传递需要编辑的数据
      this.$router.push({
        path: '/stores/add',
        query: {
          isEdit: 'true',
          id: row.id,
        },
      })
    },
    handleSizeChange(val) {
      this.query.psize = val
      this.query.page = 1 // 重置到第一页
      this.getList()
    },
    handleCurrentChange(val) {
      this.query.page = val
      this.getList()
    },
    search() {
      this.query.page = 1
      this.getList()
    },
    addStore() {
      this.$router.push('/stores/add')
    },
    refreshList() {
      this.dialogVisible = false
      this.getList()
    },
  },

  created() {
    this.getList()
  },
}
</script>

<style lang="less" scoped>
@import url(../assets/css/search.less);

.ico {
  background-color: red;
}

.searchInp {
  width: 200px;
  margin-right: 10px;
}

.el-select {
  width: 200px;
  margin-right: 10px;
}
</style>
